<?php
class Framework_DB_Owner extends Framework_DB_Common
{
	public $tables = array('SiteUser',
	                       'Owner',
	                       'FriendStatus',
	                       'InterestsInfo',
	                       'PersonalInfo', 
	                       'ContactInfo',
	                       'OwnerFile',
	                       'FileMeta',
	                       'OwnerSites',
	                       'VisitHistory',
	                       'GuestBook');
	                          
	public $refs = array(
		array(
			'ftable' => 'SiteUser',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
		
		array(
			'ftable' => 'FriendStatus',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
		array(
			'ftable' => 'FriendStatus',
			'fkey' => 'idSiteUser',
			'rtable' => 'SiteUser',
			'rkey' => 'idSiteUser',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),

		array(
			'ftable' => 'GuestBook',
			'fkey' => 'idSiteUser',
			'rtable' => 'SiteUser',
			'rkey' => 'idSiteUser',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
			
		array(
			'ftable' => 'PersonalInfo',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
		array(
			'ftable' => 'ContactInfo',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
		
		array(
			'ftable' => 'InterestsInfo',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
		array(
			'ftable' => 'OwnerFile',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
		array(
			'ftable' => 'OwnerFile',
			'fkey' => 'idFileMeta',
			'rtable' => 'FileMeta',
			'rkey' => 'idFileMeta',
			'on_update' => 'cascade',
			'on_delete' => 'set null'
		),
			
		array(
			'ftable' => 'OwnerSites',
			'fkey' => 'idSiteUser',
			'rtable' => 'SiteUser',
			'rkey' => 'idSiteUser',
			'on_update' => 'cascade',
			'on_delete' => 'cascade'
		),
			
		array(
			'ftable' => 'VisitHistory',
			'fkey' => 'idSiteUser',
			'rtable' => 'SiteUser',
			'rkey' => 'idSiteUser'
		),
		
		array(
			'ftable' => 'VisitHistory',
			'fkey' => 'idOwner',
			'rtable' => 'Owner',
			'rkey' => 'idOwner'
		),
			
		array(
			'ftable' => 'Owner',
			'fkey' => 'customCSS',
			'rtable' => 'OwnerFile',
			'rkey' => 'idOwnerFile',
			'on_update' => 'set null',
			'on_delete' => 'set null'
		),
			
		array(
			'ftable' => 'Owner',
			'fkey' => 'customHeader',
			'rtable' => 'OwnerFile',
			'rkey' => 'idOwnerFile',
			'on_update' => 'set null',
			'on_delete' => 'set null'
		),
	
	
	);

	public $links = array(
		array(
			'table1' => 'Owner',
			'table2' => 'SiteUser',
			'link'   => 'FriendStatus'
		),
			
		array(
			'table1' => 'Owner',
			'table2' => 'SiteUser',
			'link'   => 'VisitHistory'
		)
	
	
	);
	
	public $sql = array(
		// Baseline query to get an owner's data
		'getData' => array(
			'select' => '*',
			'from'   => 'SiteUser, Owner',
			'where'  => 'SiteUser.idOwner = Owner.idOwner 
						 AND Owner.loginName = ?',
			'get'    => 'row'
		),
			
	    // Gets the friend status 
	    'getFriendStatus' => array(
	    	'select' => '*',
	    	'from'   => 'FriendStatus',
	    	'where'  => 'idOwner = ? 
  						 AND idSiteUser = ?',
	    	'get'    => 'row'
	    
	    ),
	    	
	    'getAllInfo' =>  array(
	    	'select' => '*',
	    	'from'   => 'PersonalInfo, ContactInfo, InterestsInfo',
	    	'where'  => 'PersonalInfo.idOwner = ? AND ContactInfo.idOwner = ?  AND InterestsInfo.idOwner = ?'
	    ),
	    	
	    'getPersonalInfo' =>  array(
	    	'select' => '*',
	    	'from'   => 'PersonalInfo',
	    	'where'  => 'idOwner = ?'
	    ),
	    	
	    'getContactInfo' =>  array(
	    	'select' => '*',
	    	'from'   => 'ContactInfo',
	    	'where'  => 'idOwner = ?'
	    ),
	    	
	    'getInterestsInfo' =>  array(
	    	'select' => '*',
	    	'from'   => 'InterestsInfo',
	    	'where'  => 'idOwner = ?',
	    ),
	    	
	    'getCustomCSS' => array(
	    	'select' => 'filePointer', 
			'from'   => 'OwnerFile',
	    	'where'  => 'idOwnerFile = ?',
	    	'get'    => 'row'
	    ),
	    	
	    'getCustomHeader' => array(
	    	'select' => 'filePointer, extension AS ext',
	    	'from'   => 'OwnerFile, FileMeta',
	    	'where'  => 'OwnerFile.idFileMeta = FileMeta.idFileMeta
						 AND idOwnerFile = ?',
	    	'get'    => 'row'
		),
	    	
	    'getPeople' => array(
	    	'select' => '*',
	    	'from'   => 'SiteUser, FriendStatus',
	    	'where'  => 'SiteUser.idSiteUser = FriendStatus.idSiteUser
						 AND FriendStatus.idOwner = ?
						 AND FriendStatus.watchedByUser = ?
						 AND FriendStatus.watchingUser = ?',
	    	'get'    => 'all'
	    ),
	    	
	    'getPeopleList' => array(
	    	'select' => 'SiteUser.idSiteUser, SiteUser.userName',
	    	'from'   => 'SiteUser, FriendStatus',
	    	'where'  => 'SiteUser.idSiteUser = FriendStatus.idSiteUser
						 AND FriendStatus.idSiteOwner = ?
						 AND FriendStatus.watchedByUser = ?
						 AND FriendStatus.watchingUser = ?',
	    	'get'    => 'assoc'
	    ),
	    
	    'getSites' => array(
	    	'select' => '*',
	    	'from'   => 'OwnerSites',
	    	'where'  => 'idSiteUser = ?',
	    	'get'    => 'all'
	    
	    ),
	    	
	    'getFileExtensionForUploaded' => array(
	    	'select' => 'idFileMeta, extension',
	    	'from'   => 'FileMeta',
	    	'where'  => 'mime = ?',
	    	'get'    => 'row'
	    
	    ),
	    	
	    'getFileByID' => array(
	    	'select' => '*',
	    	'from'   => 'OwnerFile ,FileMeta',
	    	'where'  => 'OwnerFile.idFileMeta = FileMeta.idFileMeta 
                         AND OwnerFile.idOwnerFile = ?',
	    	'get'    => 'row'
	    
	    ),
	    	
	    'getFileByFileName' => array(
	    	'select' => '*',
	    	'from'   => 'OwnerFile, FileMeta',
	    	'where'  => 'OwnerFile.idFileMeta = FileMeta.idFileMeta 
                         AND OwnerFile.folder = ?
						 AND OwnerFile.fileName = ?
						 AND OwnerFile.idOwner = ?',
	    	'get'    => 'row'
	    
	    ),
	    	
	    'getGuestBookEntries' => array(
	    	'select' => '*',
	    	'from'   => 'SiteUser, GuestBook',
	    	'where'  => 'GuestBook.idSiteUser = SiteUser.idSiteUser
						 AND GuestBook.idOwner = ?',
			'get'    => 'all',
			'order'	 => 'GuestBook.dateAdded DESC'
	  	 )
	    	
	);
	
	public function __construct($name)
	{
		parent::__construct($name);
	}
	
	
}

?>